﻿using System;
using System.IO;

namespace BTVPodcatcherNamespace
{
    public enum DownloadStatus
    {
        Ready,
        Downloading,
        Paused,
        Complete,
        Cancelled,
        CancelRequested,
        Error,
        WaitingToWriteProperties,
        PropertiesWritten,
        FailedToWriteProperties
    }
    public class DowloadInfo
    {
        private Uri _downloadUri;
        private int _progress = -1; // percentage
        private long _speed = -1; // bytes/second

        private DownloadStatus _status;
        private FileInfo _destination;
        private long _bytesToReceive = -1, _bytesReceived = 0;

        private string _message;
        private object _tag;

        
        private bool downloadtoTempFile = true;

        private DateTime _lastUpdateTime;
        private RSSLibrary3.RSSItem _rssItem = null;

        public delegate void DownloadChangedHandler(object myObject,
                                             EventArgs myArgs);

        public event DownloadChangedHandler DownloadChanged;
        

        public virtual void ADownloadChangedOccured(EventArgs e)
          {
            if(DownloadChanged!=null)
              DownloadChanged(this, e);
          }


        public DowloadInfo()
        {

        }

        /// <summary>
        /// Creates a new DownloadEntry object.
        /// </summary>
        /// <param name="downloadUri">String representation of the download URI</param>
        /// <param name="destination">String representation of the local filename</param>
        public DowloadInfo(string downloadUri, string destination)
        {
            _downloadUri = new Uri(downloadUri);
            _destination = new FileInfo(destination);
            _status = DownloadStatus.Ready;


        }

        /// <summary>
        /// Creates a new DownloadEntry object from strong-typed parameters.
        /// </summary>
        /// <param name="downloadUri">Remote file URI</param>
        /// <param name="destination">Local file</param>
        public DowloadInfo(Uri downloadUri, FileInfo destination)
        {
            _downloadUri = downloadUri;
            _destination = destination;
            _status = DownloadStatus.Ready;
        }

        /// <summary>
        /// Number of bytes received for this entry.
        /// </summary>
        public long BytesReceived
        {
            get { return _bytesReceived; }
            set { _bytesReceived = value; }
        }

        /// <summary>
        /// Total bytes to receive.  Not valid until download has begun.
        /// </summary>
        public long BytesToReceive
        {
            get { return _bytesToReceive; }
            set { _bytesToReceive = value; }
        }

        /// <summary>
        /// Current bytes per second for this transfer.  Not valid until download has begun.
        /// </summary>
        public long Speed
        {
            get { return _speed; }
            set { _speed = value; }
        }

        /// <summary>
        /// Plain text status message for this download.
        /// </summary>
        public string Message
        {
            get { return _message; }
            set { _message = value; }
        }

        /// <summary>
        /// Local location for this file.
        /// </summary>
        public FileInfo Destination
        {
            get { return _destination; }
            set { _destination = value; }
        }

        /// <summary>
        /// Current status of this download operation.
        /// </summary>
        public DownloadStatus DownloadStatus
        {
            get { return _status; }
            set { _status = value; }
        }

        /// <summary>
        /// Progress of download from 0-100.  -1 indicates not yet begun.
        /// </summary>
        public int Progress
        {
            get { return _progress; }
            set { _progress = value; }
        }

        /// <summary>
        /// Remote file URI to be downloaded.
        /// </summary>
        public Uri DownloadUri
        {
            get { return _downloadUri; }
            set { _downloadUri = value; }
        }

        /// <summary>
        /// Indicates the last time that progress was updated during a download.
        /// </summary>
        public DateTime LastUpdateTime
        {
            get { return _lastUpdateTime; }
            set { _lastUpdateTime = value; }
        }

        /// <summary>
        /// Arbitrary data associated with this object.  Can be used to store a thread
        /// or WaitHandler as needed.
        /// </summary>
        public object Tag
        {
            get { return _tag; }
            set { _tag = value; }
        }
        public RSSLibrary3.RSSItem RSSItem
        {
            get { return _rssItem; }
            set { _rssItem = value; }
        }



        public bool useTempFile
        {
            get { return this.downloadtoTempFile; }
            set { downloadtoTempFile = value; }
        }
        public string rssFeedItemTitle
        {
            get
            {
                if (_rssItem != null)
                {
                    return _rssItem.title;
                }
                return null;
            }
        }
        public string rssFeedTitle
        {
            get
            {
                if (_rssItem != null)
                {
                    return _rssItem.rssFeed.title;
                }
                return null;
            }
        }
    }
}
